package com.glorysoft.lipuxinpda.entity;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.FragmentManager;

import com.glorysoft.lipuxinpda.R;
import com.glorysoft.lipuxinpda.activity.LoginActivity;
import com.glorysoft.lipuxinpda.base.App;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import java.io.Serializable;

/**
 * @author anyang
 * @date 2017/9/12
 * @desc
 */

public class BaseEntity<T> implements Serializable {
    private int code;
    private String message;
    private ResponseEntity<T> data;


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public ResponseEntity<T> getData() {
        return data;
    }

    public void setData(ResponseEntity<T> data) {
        this.data = data;
    }


    //获取到对象后，必须要判断是否获取成功
    public boolean isSuccess(Context context) {
        if (code == 200) {
            if (data != null) {
                if (data.getReturn().getRETURNCODE() == 0) { //新增测试用
                    if (data.getBody() != null) {
                        return true;
                    } else {
                        FreeApi_ToastUtils.showFreeToast("返回Body数据为空，请检查后台数据", context, false, Toast.LENGTH_SHORT);
                        return false;
                    }
                }else if(data.getReturn().getRETURNCODE() == 1){
                    FreeApi_ToastUtils.showFreeToast(data.getReturn().getRETURNMESSAGE(), context, false, Toast.LENGTH_SHORT);
                    FreeApi_ActivityManager.getActivityManager().finishAllActivity();
                    FreeApi_Utils.skipWithExtraIntent("", context, LoginActivity.class);
                    return false;
                } else {
                    FreeApi_ToastUtils.showFreeToast(data.getReturn().getRETURNMESSAGE(), context, false, Toast.LENGTH_SHORT);
                    return false;
                }
            } else {
                FreeApi_ToastUtils.showFreeToast("返回data数据为空，请检查后台数据", context, false, Toast.LENGTH_SHORT);
                return false;
            }

        } else {
            FreeApi_ToastUtils.showFreeToast(message, context, false, Toast.LENGTH_SHORT);
            return false;
        }
    }

    //获取到对象后，必须要判断是否获取成功
    public boolean isSuccess(FragmentManager manager) {
        if (code == 200) {
            if (data != null) {
                if (data.getReturn().getRETURNCODE() == 0) { //新增测试用
                    if (data.getBody() != null) {
                        return true;
                    } else {
                        showDialog(manager,"返回Body数据为空，请检查后台数据");
                        return false;
                    }
                }else if(data.getReturn().getRETURNCODE() == 1){
                    showDialog(manager, data.getReturn().getRETURNMESSAGE(), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                            FreeApi_ActivityManager.getActivityManager().finishAllActivity();
                            Intent intent = new Intent();
                            intent.setClass(App.Companion.getApp(), LoginActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                            App.Companion.getApp().startActivity(intent);
                            //FreeApi_Utils.skipWithExtraIntent("", App.app, LoginActivity.class);
                        }
                    });
                    return false;
                } else {
                    showDialog(manager,data.getReturn().getRETURNMESSAGE());
                    return false;
                }
            } else {
                showDialog(manager,"返回data数据为空，请检查后台数据");
                return false;
            }

        } else {
            showDialog(manager,message);
            return false;
        }
    }

    private transient FreeUI_GeneralFragmentDialog dialog;

    private void showDialog(FragmentManager manager, String text)
    {
        dialog = new FreeUI_GeneralFragmentDialog();
        View view = LayoutInflater.from(App.Companion.getApp()).inflate(R.layout.dialog_prompt, null);
        TextView tv = view.findViewById(R.id.tvTips);
        Button btnConfirm = view.findViewById(R.id.btnConfirm);
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tv.setText(text);
        dialog.setLayout(view);
        dialog.show(manager,"prompt");
        dialog.setCallBack(new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {
                dialog = null;
            }
        });
    }

    private void showDialog(FragmentManager manager, String text, View.OnClickListener onClickListener)
    {
        dialog = new FreeUI_GeneralFragmentDialog();
        View view = LayoutInflater.from(App.Companion.getApp()).inflate(R.layout.dialog_prompt, null);
        TextView tv = view.findViewById(R.id.tvTips);
        Button btnConfirm = view.findViewById(R.id.btnConfirm);
        btnConfirm.setOnClickListener(onClickListener);
        tv.setText(text);
        dialog.setLayout(view);
        dialog.show(manager,"prompt");
        dialog.setCallBack(new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {
                dialog = null;
            }
        });
    }
}
